<template>
  <div class="login-page">
    <el-card class="box-card">
      <template #header> 黑马面经运营后台 </template>
      <el-form :model="form" :rules="rules" ref="form" label-width="60px">
        <el-form-item prop="username" label="账号">
          <el-input
            v-model="form.username"
            type="text"
            placeholder="请输入账号"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password" label="密码">
          <el-input
            v-model="form.password"
            type="password"
            placeholder="请输入密码"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm">登录</el-button>
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import { loginAPI } from '@/api/user'
export default {
  name: 'LoginIndex',
  data () {
    return {
      form: {
        username: 'admin',
        password: 'admin'
      },
      rules: {
        username: [
          {
            required: true,
            message: '请输入活动名称',
            trigger: ['blur', 'change']
          },
          {
            min: 3,
            max: 5,
            message: '长度在 3 到 5 个字符',
            trigger: ['blur', 'change']
          }
        ],
        password: [
          {
            required: true,
            message: '请输入活动名称',
            trigger: ['blur', 'change']
          },
          {
            min: 3,
            max: 5,
            message: '长度在 3 到 5 个字符',
            trigger: ['blur', 'change']
          }
        ]
      }
    }
  },
  methods: {
    submitForm () {
      this.$refs.form.validate(async valid => {
        if (valid) {
          try {
            const res = await loginAPI(this.form)

            this.$store.commit('user/getToken', res.data.token)
            this.$message.success('登录成功')
            this.$router.push('/')
          } catch (error) {
            this.$message.warning(error.response.data.message)
          }
        }
      })
    },
    resetForm () {
      this.$refs.form.resetFields()
    }
  }
}
</script>

<style lang="scss" scoped>
.login-page {
  min-height: 100vh;
  background: url(@/assets/login-bg.svg) no-repeat center/cover;
  display: flex;
  justify-content: center;
  align-items: center;
  .el-card {
    width: 420px;
    ::v-deep .el-card__header {
      height: 80px;
      background: rgba(114, 124, 245, 1);
      text-align: center;
      line-height: 40px;
      color: #fff;
      font-size: 18px;
    }
  }
  .el-form {
    padding: 0 20px;
  }
  .tc {
    text-align: center;
  }
}
</style>
